//
//  HBHOrderViewController.m
//  HubeiHotel
//
//  Created by 张露杰 on 17/11/13.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HBHOrderViewController.h"
#import "OrderCell.h"
@interface HBHOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSArray *imageArr;//图片数组
@property (nonatomic,assign)BOOL isEdit;//是否编辑删除

@property (nonatomic) NSInteger pageNum;
@property (nonatomic, copy) NSMutableArray *datas;

@end

@implementation HBHOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isEdit = NO;
    _datas = [NSMutableArray new];
    [self initTitleWithTitle:@"订单"];
    [self initRightWithTitle:@"编辑" andTitleColor:[UIColor whiteColor] andFont:16];
    [self.view addSubview:self.tableV];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    [self getOrders];
}
//编辑按钮
- (void)initRightWithTitle:(NSString *)title andTitleColor:(UIColor *)titleColor andFont:(CGFloat)font{
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(onTapRight:)];
    self.navigationItem.rightBarButtonItem = barButton;
    [self.navigationItem.rightBarButtonItem setTintColor:titleColor];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] forState:UIControlStateNormal];
}

//编辑按钮的点击事件
-(void)onTapRight:(UIBarButtonItem *)btn
{
    _isEdit = !_isEdit;
    [btn setTitle:_isEdit?@"完成":@"编辑"];
    if (_isEdit) {
        _tableV.mj_header = nil;
        _tableV.mj_footer = nil;
    }else{
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _pageNum = 1;
            [_tableV.mj_footer resetNoMoreData];
            [self getOrders];
        }];
        
        _tableV.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
            _pageNum ++;
            [self getOrders];
        }];
    }
    [self.tableV reloadData];
}
-(UITableView *)tableV//创建视图
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64-49) style:UITableViewStylePlain];
        _tableV.estimatedRowHeight = 80*screenW_6;
        _tableV.backgroundColor = [UIColor whiteColor];
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _pageNum = 1;
            [_tableV.mj_footer resetNoMoreData];
            [self getOrders];
        }];
        
        _tableV.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
            _pageNum ++;
            [self getOrders];
        }];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datas.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell"];
    if (cell == nil) {
        cell = [[OrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OrderCell"];
    }
    [cell loadCell:_datas[indexPath.row] isEditing:_isEdit];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(){
        [weakSelf deleteWithIndex:indexPath.row];
    };
    
    return cell;
}

-(void)getOrders
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/business/findOrderList" Dic:@{@"jdUser.id":[Storage getToken],@"pageNo":[NSString stringWithFormat:@"%ld",_pageNum],@"pageSize":@"10"} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    if (_datas.count < [responseDic[@"count"] integerValue]) {
                        [_datas addObjectsFromArray:responseDic[@"data"]];
                        [_tableV reloadData];
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [HudHelper hideHudToView:self.view];
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    if (_datas.count >= [responseDic[@"count"] integerValue]) {
                        [_tableV.mj_footer endRefreshingWithNoMoreData];
                    }else{
                        [_tableV.mj_footer endRefreshing];
                    }
                    
                }
            });
        }];
    });
}

-(void)deleteWithIndex:(NSInteger )index
{
    UIAlertController*alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"确定删除该订单" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnullaction) {
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction) {
        [HudHelper showProgressHudWithMessage:@"" toView:self.view];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [HttpRequest PostrequestUrl:@"/business/deleteOrder" Dic:@{@"id":_datas[index][@"id"]} completion:^(NSDictionary *responseDic) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [HudHelper hideHudToView:self.view];
                    if ([responseDic[@"success"] boolValue]) {
                        [_datas removeObjectAtIndex:index];
                        [_tableV reloadData];
                    }else{
                        [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                    }
                });
            }];
        });
    }]];
    [self presentViewController:alertController animated:YES completion:^{
    }];
}

@end
